package com.ml.carManager.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.ml.carManager.R;
import com.ml.carManager.ui.dialog.listener.OnDetachedListener;

import java.util.Timer;
import java.util.TimerTask;


public class WaitDialog extends Dialog {

    private TextView content, time;
    private Context context = null;
    private String contentString = "请稍后...";
    private boolean haveTimer = false;//是否显示倒计时
    private boolean haveHintTxt = true;//是否显示提示文本
    private int overTime = 20;//默认超时时间
    private int currTime = 0;//当前时间

    private OnTimeOutListener onTimeOutListener = null;
    private OnDetachedListener onDetachedListener = null;

    public WaitDialog(Context context) {
        super(context, R.style.normal_dialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//状态栏透明
        setContentView(R.layout.dialog_wait);
        initWidget();
        setCanceledOnTouchOutside(false);
    }

    private void initWidget() {
        content = (TextView) findViewById(R.id.dialog_wait_content);
        time = (TextView) findViewById(R.id.dialog_wait_time);
    }

    /**
     * 设置内容文本
     */
    public void setContent(String c) {
        this.contentString = c;
        if (content != null) {
            content.setText(contentString);
        }
    }

    /**
     * 设置内容文本
     */
    public void setText(String c) {
        setContent(c);
    }

    Timer timer;
    DescTimerTask timerTask;

    @Override
    public void show() {
        try {
            if (!isShowing()) {
                super.show();
            }
        } catch (Exception e) {

        }
        if (haveTimer) {
            time.setVisibility(View.VISIBLE);
            // new一个计时器实例
            timer = new Timer();
            timer.schedule(timerTask = new DescTimerTask(), 0, 1000);
            currTime = overTime;
            setTime(currTime);
            setCancelable(false);
        } else
            time.setVisibility(View.GONE);

        if (haveHintTxt) {
            content.setVisibility(View.VISIBLE);
            content.setText(contentString);
        } else {
            content.setVisibility(View.GONE);
        }

    }

    class DescTimerTask extends TimerTask {
        @Override
        public void run() {
            handler.sendEmptyMessage(0);
            currTime--;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            setTime(currTime);
            if (currTime <= 0 && timer != null && timerTask != null) {
                timer.cancel();
                timerTask.cancel();
                if (onTimeOutListener != null)
                    onTimeOutListener.onTimeOut();
            }
        }
    };

    /**
     * 设置是否有计时器
     */
    public void setHaveTimer(boolean haveTimer) {
        this.haveTimer = haveTimer;
    }

    /**
     * 设置是否有提示文本
     */
    public void setHaveHintTxt(boolean haveHintTxt) {
        this.haveHintTxt = haveHintTxt;
    }

    /**
     * 设置待超时时间
     */
    public void setOverTime(int overTime) {
        this.overTime = overTime;
    }

    private void setTime(int t) {
        time.setText(t + "秒");
    }

    /**
     * 设置时间文本的文本值
     */
    public void setTimeTxt(int visiable, String txt) {
        time.setVisibility(visiable);
        content.setText(txt);
    }

    @Override
    public void dismiss() {
        if (isShowing()) {
            try {
                super.dismiss();
            } catch (Exception e) {

            }
            if (timer != null) timer.cancel();
            if (timerTask != null) timerTask.cancel();
        }
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (onDetachedListener != null) {
            onDetachedListener.onDetach();
        }
    }

    public void setOnDetachedListener(OnDetachedListener onDetachedListener) {
        this.onDetachedListener = onDetachedListener;
    }

    public void setOnTimeOutListener(OnTimeOutListener onTimeOutListener) {
        this.onTimeOutListener = onTimeOutListener;
    }

    public interface OnTimeOutListener {
        void onTimeOut();
    }
}
